﻿using SockLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Socket服务器
{
    class SocketConfig
    {
      private static readonly   string   path = Directory.GetCurrentDirectory() + "\\Config\\Serverconfig.xml";

        /// <summary>
        /// 读取服务器配置
        /// </summary>
        /// <returns></returns>
        public static DataTable ReadConfig()
        {
            DataTable DT = new DataTable();
            DT.Columns.Add("ServerName");
            DT.Columns.Add("ServerPort");
            DT.Columns.Add("ServerCapacity");
            DT.Columns.Add("ServerIp");
            try
            {
                if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Config"))
                    Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Config");
                if (!File.Exists(path))
                {
                    //File.Create(path);
                    XDocument xdoc = new XDocument();
                    xdoc.Add(new XElement("Config"));
                    xdoc.Save(path);
                }
                else
                    ReadXml(XElement.Load(path), ref DT);               
            return DT;

            }
            catch (Exception ex)
            {
                logexception.WriteLogToFile(ex);
                return DT;
            }
        }

        static void  ReadXml(XElement doc,ref DataTable DT)
        {
            var linq = from xml in doc.Elements()
                       select xml;
            foreach (XElement  item in linq )
            {
                DataRow DR = DT.NewRow();
                DR["ServerName"] = item.Attribute("ServerName").Value ;
                DR["ServerPort"] =Convert.ToInt32( item.Attribute("ServerPort").Value);
                DR["ServerCapacity"] = Convert.ToInt32(item.Attribute("ServerCapacity").Value); 
                DR["ServerIp"] = item.Attribute("ServerIp").Value;
                DT.Rows.Add(DR);
            } 
        }

        public static void  AddServers(string ServerName,string ServerPort,string ServerCapacity,string ServerIp)
        {
            try
            {
                XElement Root = XElement.Load(path);
                var linq = from xml in Root.Elements()
                           select xml;
                foreach (XElement item in linq)
                {
                    if (item.Attribute("ServerPort").Value == ServerPort )
                        return;
                }
                Root.Add(new XElement ("Servers",
                    new XAttribute("ServerPort", ServerPort),
                    new XAttribute("ServerName", ServerName),
                    new XAttribute("ServerCapacity", ServerCapacity),
                    new XAttribute("ServerIp", ServerIp)));
                Root.Save(path);
            }

            catch (Exception ex)
            {
                logexception.WriteLogToFile(ex);
            }
        }

        public static void DelServers( string ServerPort,string ServerIp)
        {
            try
            {
                XElement Root = XElement.Load(path);
                var linq = from xml in Root.Elements()
                           select xml;
                foreach (XElement item in Root.Elements () )
                {
                    if (item.Attribute("ServerPort").Value == ServerPort )
                        item.Remove();
                }
                Root.Save(path);
            }
            catch (Exception ex)
            {
                logexception.WriteLogToFile(ex);
            }
        }
    }
}
